package com.hcmus.arLocation;

import java.util.List;

import org.json.JSONObject;

import com.hcmus.armobile.R;

import db.DBHandle;

import android.content.Intent;
import android.graphics.Color;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class DetailLocation extends AugmentedActivity {
	private TextView titleShopping = null;
	private TextView addressShopping = null;
	private TextView promotionShopping = null;
	private Button btnCall = null;
	private Button btnGetDirection = null;
	private Button btnShare = null;
	private Button btnChain = null;
	private ImageButton btnBack = null;
	private String chainShoppingName = "";
	private String idChainShopping = "";
	private String lng = "";
	boolean isChainShopping = true;
	private String lat = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detaillocation);
		this.titleShopping = (TextView) findViewById(R.id.title_store_name);
		this.addressShopping = (TextView) findViewById(R.id.contentAddress);
		this.promotionShopping = (TextView) findViewById(R.id.contentPromotion);
		this.btnCall = (Button) findViewById(R.id.btnCall);
		this.btnGetDirection = (Button) findViewById(R.id.btnDirection);
		this.btnShare = (Button) findViewById(R.id.btnShare);
		this.btnChain = (Button) findViewById(R.id.btnChainShop);
		this.btnBack = (ImageButton) findViewById(R.id.btnBackDetailLocation);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			
			/*begin check and delete when user watched promotion*/
			boolean isDelete = extras.getBoolean("delete_promotion");
			if(isDelete){
				int p_id = extras.getInt("promotion_id");
				DBHandle.Delete(" delete ar_promotion where id = "+p_id);				
			}
			/*end check and delete when user watched promotion*/
			
			this.titleShopping.setText(extras.getString("location_name_to"));
			this.addressShopping.setText(extras
					.getString("location_address_to"));
			this.promotionShopping.setText(extras
					.getString("location_promotion_to"));
			this.setIdChainShopping(extras
					.getString("location_Location_type_to"));
			this.setChainShoppingName(extras
					.getString("location_ChainShopName_to"));
			this.setLng(extras.getString("location_lon_to"));
			this.setLat(extras.getString("location_lat_to"));
			Toast.makeText(getApplicationContext(),
					getIdChainShopping() + "-----" + getChainShoppingName(), 10)
					.show();
			if (getChainShoppingName() != null && getChainShoppingName() != ""
					&& getChainShoppingName() == "Retail") {
				this.btnChain.setBackgroundColor(Color.GRAY);
				this.btnChain.setTextColor(Color.BLACK);
				isChainShopping = false;
			}
		}

		this.btnCall.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// show intent call
			}

		});

		this.btnChain.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (isChainShopping == false) {
					return;
				}
				List<Marker> markers = null;
				try {
					DentifierDataSource src = null;

					src = new DentifierDataSource(getResources(),
							getApplicationContext());

					

					JSONObject objData = DentifierDataSource
							.LoadDataSource("SELECT * FROM LocationInformation li LEFT JOIN LocationType lt ON(li.IDLocationShoppingType = lt.IDLocationType) WHERE li.IDLocationShoppingType = "
									+ getIdChainShopping()
									+ " ORDER BY li.ID ASC");

					markers = src.parseNonNetwork(objData);

					//Toast.makeText(v.getContext(), markers.size(), 10).show();
					// Toast.makeText(getApplicationContext(),getIdChainShopping(),10).show();

					ARData.clearMarkers();

					ARData.addMarkers(markers);

					Intent in = new Intent(v.getContext(),
							GeoMappingMarker.class);
					startActivity(in);
				} catch (Exception ex) {
					Toast.makeText(getApplicationContext(),"Size Markers : " + markers.size() +
							"eeee" + ex.toString(), 30).show();
				}
			}
		});

		this.btnGetDirection.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// show intent Get Direction
				Intent in = new Intent(v.getContext(), GetDirectionOption.class);
				in.putExtra("location_name_to", titleShopping.getText());
				in.putExtra("location_lon_to", getLng());
				in.putExtra("location_lat_to", getLat());
				startActivity(in);
			}

		});

		this.btnShare.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// show intent Share				
				Intent iFace = new Intent(v.getContext(),com.hcmus.facebook.MainFace.class );
				startActivity(iFace);
				
			}

		});

		this.btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent in = new Intent(v.getContext(), ArLocation.class);
				startActivity(in);
				return;
			}

		});
	}

	private String getChainShoppingName() {
		return chainShoppingName;
	}

	private void setChainShoppingName(String chainShoppingName) {
		this.chainShoppingName = chainShoppingName;
	}

	private String getIdChainShopping() {
		return idChainShopping;
	}

	private void setIdChainShopping(String idChainShopping) {
		this.idChainShopping = idChainShopping;
	}

	private String getLng() {
		return lng;
	}

	private void setLng(String lng) {
		this.lng = lng;
	}

	private String getLat() {
		return lat;
	}

	private void setLat(String lat) {
		this.lat = lat;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent in = new Intent(this, ArLocation.class);
			startActivity(in);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
